package com.znsd.zl.app.wechat.controller;

import com.znsd.zl.app.user.annotation.Login;
import com.znsd.zl.app.user.service.IAppUserService;
import com.znsd.zl.app.user.utils.JwtUtils;
import com.znsd.zl.app.wechat.service.IWeChatPayService;
import com.znsd.zl.app.wechat.vo.req.WeChatPayReqVO;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *     微信支付统一下单接口
 * </p>
 *
 * @ClassName WeChatPay
 * @Description 微信支付下单
 * @Author jaypdd
 * @Date 2020/8/27 21:39
 * @Version v1.0
 */
@RestController
@RequestMapping("/user/weChat")
@Slf4j
public class WeChatPayController {

	@Resource
	private IWeChatPayService payService;

	@Resource
	private IAppUserService userService;

	@Resource
	private JwtUtils jwtUtils;

	/**
	 * 微信支付统一下单接口
	 * @param reqVO     商户订单信息VO
	 * @param request   http请求对象，获取用户令牌
	 * @return          返回微信支付唤签名等参数
	 */
	@Login
	@PostMapping("/pay")
	public R pay(@RequestBody WeChatPayReqVO reqVO, HttpServletRequest request) {
		// 解密令牌获取令牌中的用户openId
		String token = request.getHeader("token");
		reqVO.setOpenId(jwtUtils.tokenDecode(token)[2]);
		try {
			return R.ok().put("data", payService.appPay(reqVO));
		} catch (Exception exception) {
			log.error("【微信小程序】微信支付下单失败");
			return R.error("支付失败");
		}
	}

	/**
	 * 微信支付成功回调
	 * @return
	 */
	@PostMapping("/payNotify")
	public R payNotify() {
		log.info("【微信小程序】支付成功，微信回调");
		return R.ok();
	}

}

